home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 778 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
  2. Message-ID: <314E1D5D.76E5@cs.tu-berlin.de>
  3. X-Original-Date: Tue, 19 Mar 1996 03:35:09 +0100
  4. Path: in1.uu.net!bounce-back
  5. Date: 19 Mar 96 10:24:48 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Constructors and conversion operator
  9. Organization: Technical University of Berlin
  10. References: <314CD29A.3438@cs.tu-berlin.de> <4ikkp8$b5u@engnews1.Eng.Sun.COM>
  11. X-Mailer: Mozilla 2.0 (Win95; I)
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMU6LeeEDnX0m9pzZAQGNSQF+OtaCiBobZZlR0UUd9/RPcuu5CryL6KfL
  14.     A4SIuO1eUcllMakDAjyYB2EhEfUwn0tH
  15.     =XONZ
  16.  
  17. Steve Clamage wrote:
  18. > In article 3438@cs.tu-berlin.de, Roman Lechtchinsky
  19. > <wolfro@cs.tu-berlin.de> writes:
  20. > >
  21. > >I have a question about conversion operators and constructors. Consider the
  22. > >following:
  23. > >
  24. > >class X
  25. > >{
  26. > >       X( Y );
  27. > >};
  28. > >
  29. > >class Y
  30. > >{
  31. > >       operator X();
  32. > >};
  33. > A constructor taking a single argument is a type conversion operator from
  34. > the argument type to the class type. The arrangement you show is always
  35. > going to lead to ambiguity errors. There are two ways to convert a Y
  36. > to an X, and neither is preferred over the other. Operator conversion
  37. > functions should normally be avoided, partly for this reason, and partly
  38. > because they can wind up being used in surprising circumstances. The
  39. > compiler might find a conversion that allows erroneous code to compile.
  40. > >Now the second one:
  41. > >
  42. > >class A
  43. > >{
  44. > >               A(B);
  45. > >};
  46. > >
  47. > >class B
  48. > >{
  49. > >               operator A&();
  50. > >};
  51. > This is the same situation. There is no syntactic difference between
  52. > using a reference or an object. Once again, there are two ways to
  53. > make an A out of a B, and neither is preferred.
  54.  
  55. First of all, sorry: there is a mistake in the example, the constructor 
  56. should be A(B&). However, it doesn't matter since both cases are only a 
  57. variation of the following problem:
  58.  
  59. The DWP defines:
  60.  
  61. A constructor declared without the  function-specifier  explicit  that
  62.   can  be called with a single parameter specifies a conversion from the
  63.   type of its first parameter to the type of its  class. [class.conv.ctor]
  64.  
  65. Now, to me this means that a copy constructor is a converting constructor 
  66. since it takes a single parameter which is a reference to an object of the 
  67. class ( I don't think that this is intended ). If this is true a copy 
  68. constructor is a user-defined conversion ( after all, A and A& are different 
  69. types, aren't they ). This implies that the following call to foo:
  70.  
  71. foo(b)
  72.  
  73. is ill-formed ( not ambiguous ) since it requires either b=>B(b)=>A(B(b)) or 
  74. b=>A&(b)=>A(A&(b)) but only one user-defined conversion may be applied ( note 
  75. that foo is defined as foo(A), not foo(A&) ). 
  76.  
  77.  
  78. Bye
  79.  
  80. Roman
  81. ---
  82. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  83. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  84. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  85. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  86. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  87.